package com.dynamic.idea.quick.develop.plugin.entity.base02.model;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.List;

/**
 * 导入的包结构
 */
public class ImportStruct implements Serializable {

    /**
     * 包名
     */
    protected String packageName;

    /**
     * 类名
     */
    protected String className;

    /**
     * 泛型的结构列表 比如 List<String> String就是超类
     * Map<Integer,Long> 则 Integer,Long就是超类
     */
    protected List<ImportStruct> genericsClassStructList;

    /**
     * 得到权限定名
     * @return
     */
    public String getFullClassName(){
        return StringUtils.trim(packageName)+"."+StringUtils.trim(className);
    }

    /**
     * 得到全限定名
     * @return
     */
    public String showQualifiedName(){
        if (CollectionUtils.isEmpty(genericsClassStructList)){
            return packageName+"."+className;
        }else {
            StringBuilder simpleSb=new StringBuilder();
            StringBuilder allSb=new StringBuilder();

            if (StringUtils.isNotBlank(className)){
                simpleSb.append(className);
                allSb.append(packageName+"."+className);
            }
            simpleSb.append("<");
            allSb.append("<");

            for(int i=0;i<genericsClassStructList.size();i++){
                ImportStruct importStruct=genericsClassStructList.get(i);
                if (StringUtils.isBlank(importStruct.getClassName())){
                    continue;
                }
                if (!StringUtils.endsWith(simpleSb.toString(),"<")) {
                    simpleSb.append(",");
                    allSb.append(",");
                }
                simpleSb.append(importStruct.getClassName());
                allSb.append(importStruct.getPackageName()+"."+importStruct.getClassName());
            }
            simpleSb.append(">");
            allSb.append(">");
            return allSb.toString();
        }
    }

    /**
     * 简单类名
     * @return
     */
    public String showSimpleName(){
        if (CollectionUtils.isEmpty(genericsClassStructList)){
            return className;
        }else {
            StringBuilder simpleSb=new StringBuilder();
            StringBuilder allSb=new StringBuilder();

            if (StringUtils.isNotBlank(className)){
                simpleSb.append(className);
                allSb.append(className);
            }
            simpleSb.append("<");
            allSb.append("<");

            for(int i=0;i<genericsClassStructList.size();i++){
                ImportStruct importStruct=genericsClassStructList.get(i);
                if (StringUtils.isBlank(importStruct.getClassName())){
                    continue;
                }

                if (!StringUtils.endsWith(simpleSb.toString(),"<")) {
                    simpleSb.append(",");
                    allSb.append(",");
                }
                simpleSb.append(importStruct.getClassName());
                allSb.append(importStruct.getClassName());
            }
            simpleSb.append(">");
            allSb.append(">");
            return allSb.toString();
        }
    }

    /**
     * 是否需要声明导入类
     * 目前仅 java.lang 不需要导入
     * @return
     */
    public boolean needImportClass(){
        if (StringUtils.equals(packageName,"java.lang")){
            return false;
        }
        return true;
    }

    // get/set方法


    public String getPackageName() {
        return packageName;
    }

    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public List<ImportStruct> getGenericsClassStructList() {
        return genericsClassStructList;
    }

    public void setGenericsClassStructList(List<ImportStruct> genericsClassStructList) {
        this.genericsClassStructList = genericsClassStructList;
    }
}
